package com.ruoyi.web.controller.cs;

import com.ruoyi.common.core.controller.BasicsController;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.page.ResponsePageInfo;
import com.ruoyi.cs.domain.Banner;
import com.ruoyi.cs.service.BannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Api(tags = {"活动banner"})
@RestController
@RequestMapping("/cs/banner")
public class TBannerController extends BasicsController {
    @Autowired
    private BannerService bannerService;

    @ApiOperation("创建活动banner")
    @PreAuthorize("@ss.hasPermi('cs:banner:add')")
    @PostMapping("/add")
    public Response createBanner(@RequestBody Banner banner) {
        bannerService.createBanner(banner);
        return Response.success("创建成功");
    }

    @ApiOperation("更新活动banner")
    @PreAuthorize("@ss.hasPermi('cs:banner:update')")
    @PostMapping("/update")
    public Response updateBanner(@RequestBody Banner banner) {
        banner.setId(banner.getId());
        bannerService.updateBanner(banner);
        return Response.success("更新成功");
    }

    @ApiOperation("删除活动banner")
    @PreAuthorize("@ss.hasPermi('cs:banner:remove')")
    @DeleteMapping("/remove/{id}")
    public Response deleteBanner(@PathVariable Long id) {
        bannerService.deleteBannerById(id);
        return Response.success("删除成功");
    }

    @ApiOperation("获取活动banner详情")
    @PreAuthorize("@ss.hasPermi('cs:banner:info')")
    @GetMapping("/info/{id}")
    public Response<Banner> getBannerById(@PathVariable Long id) {
        return Response.success(bannerService.findBannerById(id));
    }

    @ApiOperation("活动banner列表")
    @PreAuthorize("@ss.hasPermi('cs:banner:list')")
    @PostMapping("/list")
    public ResponsePageInfo<Banner> getAllBanners(@RequestBody  Banner banner) {
        startPage(banner.getPageNum(),banner.getPageSize());
        List<Banner> list =  bannerService.findAllBanners();
        return toResponsePageInfo(list);
    }

    /******************************前端*********************************/
    @ApiOperation("前端活动banner列表")
    @GetMapping("/common/list")
    public List<Banner> commonAllBanners() {
        return bannerService.findAllBanners();
    }
}
